﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;

namespace Jb.Web.Controllers
{
    public class FileDownloadController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public FileDownloadController()
		{
		}

		public FileDownloadController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods

		/// <summary>
        /// Downloads Resume
        /// -1 -> User is not authorized to download this resume.
        /// -2 -> Haven't subscribed to any package
        /// -3 -> Package Subscription has expired
        /// +1 -> Requested content was not found for download 
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult DownloadResume(int id)
        {
            ///Only Logged in users can download resume with several criteria specific to different user.
            if (SessionCache.CurrentUser == null) 
                return View("Index", -1);
            var resume = _Context.Resumes.FirstOrDefault(P => P.ID == id);
            if (resume != null)
            {
                String filePath = WebUtil.GetResumePath(resume.JobSeekerID, resume.FileName);
                if (System.IO.File.Exists(filePath))
                {
                    ///Update the download count if this is company user.
                    if (UserInfo.IsCompanyUser)
                    {
                        if (LoggedInUser.EmployerInfo.Subscription == null)
                            return View("Index", -2);
                        else if (LoggedInUser.EmployerInfo.Subscription.CanDownloadResume)
                        {
                            SubscriptionHistory history = new SubscriptionHistory();
                            history.PackageID = LoggedInUser.EmployerInfo.Subscription.PackageID;
                            history.CompanyID = LoggedInUser.Contact.CompanyID;
                            history.ResumeID = resume.ID;                            
                            history.CreatedOn = DateTime.Now;
                            _Context.AddToSubscriptionHistories(history);

                            resume.DownloadedCount += 1;
                            LoggedInUser.EmployerInfo.Subscription.ResumeDownloaded += 1;

                            _Context.SaveChanges();
                        }
                        else
                            return View("Index", -3);
                    }
                    ///JobSeekers can download only their own resume
                    else if (UserInfo.IsJobSeeker)
                    {
                        if (resume.JobSeekerID != LoggedInUser.JobSeekerID)
                            return View("Index", -1);
                    }
                    System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
                    return File(fs, SiteConfigReader.GetContentType(System.IO.Path.GetExtension(filePath)), resume.FileName);
                }
            }
            return View("Index", 1);
        }		
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}


